<template>
  <div class="order-list-container">
    <el-card class="order-list-card">
      <template #header>
        <div class="card-header">
          <span class="title">订单列表</span>
          <el-button-group>
            <el-button type="primary" :loading="loading" @click="refreshOrders">
              <el-icon><Refresh /></el-icon>
              刷新
            </el-button>
          </el-button-group>
        </div>
      </template>

      <el-table
        :data="orderList"
        style="width: 100%"
        v-loading="loading"
        border
        stripe
      >
        <el-table-column type="index" label="序号" width="80" align="center" />
        
        <el-table-column prop="id" label="订单编号" width="200" show-overflow-tooltip>
          <template #default="scope">
            <span class="order-number">{{ scope.row.id }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="orderGenerationTime" label="下单时间" width="180">
          <template #default="scope">
            {{ formatDateTime(scope.row.orderGenerationTime) }}
          </template>
        </el-table-column>

        <el-table-column prop="paymentTime" label="支付时间" width="180">
          <template #default="scope">
            {{ scope.row.paymentTime ? formatDateTime(scope.row.paymentTime) : '未支付' }}
          </template>
        </el-table-column>

        <el-table-column prop="paymentAmount" label="订单金额" width="120" align="right">
          <template #default="scope">
            <span class="amount">¥ {{ formatNumber(scope.row.paymentAmount) }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="paymentMethod" label="支付方式" width="120" align="center">
          <template #default="scope">
            {{ formatPaymentMethod(scope.row.paymentMethod) }}
          </template>
        </el-table-column>

        <el-table-column prop="paymentStatus" label="支付状态" width="120" align="center">
          <template #default="scope">
            <el-tag :type="getPaymentStatusType(scope.row.paymentStatus)">
              {{ formatPaymentStatus(scope.row.paymentStatus) }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="120" align="center" fixed="right">
          <template #default="scope">
            <el-button
              type="primary"
              link
              @click="viewOrderDetail(scope.row.id)"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :layout="'total, sizes, prev, pager, next'"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getDriverOrders } from '@/api/order'
import { ElMessage } from 'element-plus'
import { Refresh } from '@element-plus/icons-vue'
import dayjs from 'dayjs'

const router = useRouter()
const loading = ref(false)
const orderList = ref([])
const unpaidOrderList = ref([])
const paidOrderList = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 获取订单列表数据
const fetchOrdersList = async () => {
  loading.value = true
  try {
    const res = await getDriverOrders()
    if (res.code === 1) {
      unpaidOrderList.value = res.data.unpaidOrderList || []
      paidOrderList.value = res.data.paidOrderList || []
      // 合并订单列表用于显示
      orderList.value = [...unpaidOrderList.value, ...paidOrderList.value]
      total.value = orderList.value.length
    } else {
      ElMessage.error(res.msg || '获取订单列表失败')
    }
  } catch (error) {
    console.error('获取订单列表失败：', error)
    ElMessage.error('获取订单列表失败')
  } finally {
    loading.value = false
  }
}

// 刷新订单列表
const refreshOrders = () => {
  fetchOrdersList()
}

// 查看订单详情
const viewOrderDetail = (id) => {
  router.push({
    name: 'OrderDetail',
    params: { id }
  })
}

// 格式化日期时间
const formatDateTime = (date) => {
  return dayjs(date).format('YYYY-MM-DD HH:mm')
}

// 格式化金额
const formatNumber = (num) => {
  return Number(num).toLocaleString('zh-CN', {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
  })
}

// 格式化支付方式
const formatPaymentMethod = (method) => {
  const methodMap = {
    1: '支付宝',
    2: '微信',
    3: '银行卡'
  }
  return methodMap[method] || '未知'
}

// 格式化支付状态
const formatPaymentStatus = (status) => {
  const statusMap = {
    0: '未支付',
    1: '已支付',
    2: '支付失败'
  }
  return statusMap[status] || '未知'
}

// 获取支付状态标签类型
const getPaymentStatusType = (status) => {
  const typeMap = {
    0: 'warning',
    1: 'success',
    2: 'danger'
  }
  return typeMap[status] || 'info'
}

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val
  currentPage.value = 1
  fetchOrdersList()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchOrdersList()
}

// 页面加载时获取数据
onMounted(() => {
  fetchOrdersList()
})
</script>

<style scoped>
.order-list-container {
  padding: 20px;
}

.order-list-card {
  min-height: calc(100vh - 100px);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 16px;
  font-weight: bold;
}

.order-number {
  font-family: monospace;
  color: #409EFF;
}

.amount {
  font-weight: bold;
  color: #67c23a;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

:deep(.el-card__header) {
  padding: 15px 20px;
}

:deep(.el-table) {
  margin-top: 20px;
}

:deep(.el-table__header) {
  background-color: #f5f7fa;
}

:deep(.el-pagination) {
  justify-content: flex-end;
}
</style> 